package com.qianxun.module.datasource1.admin.view;

import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.init.bo.AdminLoginType;
import com.qianxun.module.datasource1.admin.entity.SysUser;
import com.qianxun.module.base.exception.ServiceInterruptionException;
import com.qianxun.module.base.view.BaseLoginViewController;
import com.qianxun.module.datasource1.common.constant.CaptchaValidObjectEnum;
import com.qianxun.module.datasource1.common.manager.CaptchaValidObjectHolder;
import com.qianxun.plugins.shiro.LoginTypeToken;
import com.qianxun.plugins.shiro.ShiroUtil;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author: huwei
 * @date: 2019/11/11 17:27
 * @version: 1.0.0
 */
@Slf4j
@Controller
@RequestMapping(value = {"/admin"})
public class AdminBaseViewController extends BaseLoginViewController {

    private static final String VIEW_PAGE_PREFIX = "module/admin";

    @Override
    protected String prefix() {
        return VIEW_PAGE_PREFIX;
    }

    @Override
    public ModelAndView index(ModelAndView modelAndView , HttpServletRequest request , HttpServletResponse response) {
        //设置当前用户信息
        modelAndView.addObject("admin", ObjectUtil.toMapEx(ShiroUtil.getUser(SysUser.class),"password"));
        return super.index(modelAndView ,request ,response);
    }

    @Override
    protected LoginTypeToken getCustomShiroToken(String username, String password) {
        return new LoginTypeToken(username, password, new AdminLoginType());
    }

    @RequestMapping(value = {"/loading"})
    public ModelAndView loading(ModelAndView modelAndView) {
        modelAndView.setViewName(prefix().concat("/loading.html"));
        return modelAndView;
    }

    /**
     * 通用的树列表选择器
     *
     * @param formName
     * @param formId
     * @param treeUrl
     * @param model
     * @return
     */
    @RequestMapping(value = {"/commonTree"})
    public String deptTreeList(@RequestParam("formName") String formName,
                               @RequestParam("formId") String formId,
                               @RequestParam("treeUrl") String treeUrl, Model model) {

        if (StringUtils.isEmpty(formName) || StringUtils.isEmpty(formId) || StringUtils.isEmpty(treeUrl)) {
            throw ServiceInterruptionException.fail(400, "参数错误");
        }

        try {
            model.addAttribute("formName", URLDecoder.decode(formName, "UTF-8"));
            model.addAttribute("formId", URLDecoder.decode(formId, "UTF-8"));
            model.addAttribute("treeUrl", URLDecoder.decode(treeUrl, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw ServiceInterruptionException.fail(400, "参数错误");
        }

        return "template/tree_dlg.html";
    }

    @RequestMapping(value = {"/toTheme"})
    public ModelAndView toTheme(ModelAndView modelAndView) {
        modelAndView.setViewName("template/theme.html");
        return modelAndView;
    }

    @Override
    public ModelAndView login(ModelAndView modelAndView, String username, String password, HttpServletRequest request, HttpServletResponse response) {
        if(CaptchaValidObjectHolder.getInstance().isValid(CaptchaValidObjectEnum.LOGIN.getCode())){
            String code = request.getParameter("code");
            try {
                boolean b = CaptchaUtil.ver(code ,request);
                if(b){
                    return super.login(modelAndView, username, password, request, response);
                }
            }catch (Throwable throwable){
            }
            return toLogin(modelAndView ,"验证码验证失败，请刷新并重试" ,request ,response);
        }else{
            return super.login(modelAndView, username, password, request, response);
        }
    }
}
